﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Identity;
using WeChat.CMS.Controllers;
using WeChat.CMS.OrganizationUnits;
using WeChat.CMS.Volo.Identity;

namespace WeChat.CMS.Users
{

    [RemoteService(Name = "User")]
    [ControllerName("User")]
    [Route("api/identity/users")]
    public  class UserController: CMSController, IUserAppService
    {
        protected IUserAppService UserAppService { get; }
        public UserController(IUserAppService userAppService)
        {
            UserAppService = userAppService;
        }
        [HttpPost]
        [Route("create-to-organization")]
        public Task<IdentityUserDto> CreateAsync(IdentityUserOrgCreateDto input)
        {
            return UserAppService.CreateAsync(input);
        }
        [HttpPost]
        [Route("add-user-to-organization")]
        public Task<bool> AddToOrganizationUnitAsync(UseToOrganizationUnitCreationDto input)
        {
            return UserAppService.AddToOrganizationUnitAsync(input);
        }
        [HttpGet]
        [Route("user-in-organization")]
        public Task<PagedResultDto<IdentityUserDto>> GetUsersInOrganizationUnitAsync(OrganizationUnitUsersPagedListDto input)
        {
            return UserAppService.GetUsersInOrganizationUnitAsync(input);
        }
        [HttpDelete]
        [Route("remove-user-from-organization")]
        public Task<bool> RemoveFromOrganizationUnitAsync(UseToOrganizationUnitDeleteDto input)
        {
            return UserAppService.RemoveFromOrganizationUnitAsync(input);
        }
        [HttpGet]
        [Route("{id}/organizations")]
        public Task<ListResultDto<OrganizationUnitEto>> GetListOrganizationUnitsAsync(Guid id, bool includeDetails = false)
        {
            return UserAppService.GetListOrganizationUnitsAsync(id, includeDetails);
        }
        [HttpPut]
        [Route("{id}/update-to-organizations")]
        public Task<IdentityUserDto> UpdateAsync(Guid id, IdentityUserOrgUpdateDto input)
        {
            return UserAppService.UpdateAsync(id, input);
        }
        [HttpGet]
        [Route("all")]
        public async Task<List<IdentityUserDto>> GetListAllAsync()
        {
            return await UserAppService.GetListAllAsync();
        }
        [HttpPut]
        [Route("updateuseractive")]
        public async Task<bool> UpdateUseActiveAsync(UpdateUseActiveDto input)
        {
            return await UserAppService.UpdateUseActiveAsync(input);
        }
        [Authorize(CMSIdentityPermissions.Users.LoclUnLock)]
        [HttpPut]
        [Route("unlockuser/{id}")]
        public async Task<bool> UpdateUseLockAsync(Guid id)
        {
            return await UserAppService.UpdateUseLockAsync(id);
        }
        [Authorize(CMSIdentityPermissions.Users.ActiveIsActive)]
        [HttpPost]
        [Route("updateavator")]
        public async Task<string> UpdateAvatorAsync([FromForm] UpdateAvatorDto input)
        {
            return await UserAppService.UpdateAvatorAsync(input);
        }
        [Authorize(IdentityPermissions.Users.Delete)]
        [HttpDelete]
        [Route("deleteuser/{userid}")]
        public async Task<bool> DeleteUseInfo(Guid userid)
        {
            return await UserAppService.DeleteUseInfo(userid);
        }
    }
}
